package com.mashiro.system.controller;

import com.mashiro.common.annotation.Log;
import com.mashiro.common.core.controller.BaseController;
import com.mashiro.common.core.domain.AjaxResult;
import com.mashiro.common.core.domain.entity.SysRole;
import com.mashiro.common.core.domain.model.LoginUser;
import com.mashiro.common.enums.BusinessType;
import com.mashiro.common.utils.SecurityUtils;
import com.mashiro.common.utils.poi.ExcelUtil;
import com.mashiro.system.domain.MoeFund;
import com.mashiro.system.service.IMoeFundService;
import com.mashiro.system.service.IMoeProjectService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * 资金Controller
 *
 * @author mashiro
 * @date 2023-04-15
 */
@RestController
@RequestMapping("/system/fund")
public class MoeFundController extends BaseController {
    @Resource
    private IMoeFundService moeFundService;

    @Resource
    private IMoeProjectService moeProjectService;


    /**
     * 查询资金列表
     */
    @PreAuthorize("@ss.hasPermi('system:fund:list')")
    @GetMapping("/list")
    public AjaxResult list(MoeFund moeFund) {
        AtomicBoolean isManager = new AtomicBoolean(false);
        AtomicBoolean isAdmin = new AtomicBoolean(false);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        List<SysRole> roles = loginUser.getUser().getRoles();
        List<MoeFund> list = null;
        roles.forEach(sysRole -> {
            if (11L == sysRole.getRoleId()){
                isManager.set(true);
            }else if (1L == sysRole.getRoleId() || 10L == sysRole.getRoleId()){
                isAdmin.set(true);
            }
        });
        if (!isAdmin.get()){
            if (isManager.get()){
                Long[] projectIds = moeProjectService.getMoeProjectIdsByManagerId(SecurityUtils.getUserId());
                list = moeFundService.getMoeFundListByProjectIds(moeFund, projectIds);
            }
        }else {
            list = moeFundService.selectMoeFundList(moeFund);
        }
        return success(list);
    }

    /**
     * 导出资金列表
     */
    @PreAuthorize("@ss.hasPermi('system:fund:export')")
    @Log(title = "资金", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MoeFund moeFund) {
        List<MoeFund> list = moeFundService.selectMoeFundList(moeFund);
        ExcelUtil<MoeFund> util = new ExcelUtil<MoeFund>(MoeFund.class);
        util.exportExcel(response, list, "资金数据");
    }

    /**
     * 获取资金详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:fund:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(moeFundService.selectMoeFundById(id));
    }

    /**
     * 新增资金
     */
    @PreAuthorize("@ss.hasPermi('system:fund:add')")
    @Log(title = "资金", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MoeFund moeFund) {
        return toAjax(moeFundService.insertMoeFund(moeFund));
    }

    /**
     * 修改资金
     */
    @PreAuthorize("@ss.hasPermi('system:fund:edit')")
    @Log(title = "资金", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MoeFund moeFund) {
        return toAjax(moeFundService.updateMoeFund(moeFund));
    }

    /**
     * 删除资金
     */
    @PreAuthorize("@ss.hasPermi('system:fund:remove')")
    @Log(title = "资金", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        if (moeFundService.hasChildByFundId(id)) {
            return warn("存在子菜单,不允许删除");
        }
        return toAjax(moeFundService.deleteMoeFundById(id));
    }

    /**
     * 是否存在资金子节点
     *
     * @param projectId 项目ID
     * @param taskId 任务ID
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('system:fund:query')")
    @GetMapping(value = "/project/{projectId}/task/{taskId}")
    public AjaxResult getFundInfo(@PathVariable("projectId") Long projectId, @PathVariable(value = "taskId") Long taskId) {
        return success(moeFundService.getFundInfo(projectId, taskId));
    }

    /**
     * 计算项目已使用资金情况
     *
     * @param projectId 项目ID
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('system:fund:query')")
    @GetMapping(value = "/projectUsed/{projectId}")
    public AjaxResult getProjectUsedFunding(@PathVariable("projectId") Long projectId) {
        return success(moeFundService.getProjectUsedFunding(projectId));
    }

}
